package com.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.DealDao;
import com.enetiy.Balance;
import com.enetiy.Balanceorder;
import com.enetiy.Keeptime;
import com.enetiy.Ticketuser;
import com.enetiy.Transaction;
import com.enetiy.Vipmember;
import com.enetiy.Viprecord;
import com.util.PagingUilt;

public class DealDaoimpl extends HibernateDaoSupport implements DealDao {

	@Override
	public Serializable insetrTransaction(Transaction transaction)
			throws Exception {
		return this.getHibernateTemplate().save(transaction);
	}
	@Override
	public Serializable insetrVipmember(Vipmember vipmember) throws Exception {
		return this.getHibernateTemplate().save(vipmember);
	}

	@Override
	public void deleteVipmember(Vipmember vipmember) throws Exception {
		this.getHibernateTemplate().delete(vipmember);
	}

	@Override
	public Serializable insertBalance(Balance balance) throws Exception {
		return this.getHibernateTemplate().save(balance);
	}

	@Override
	public void updateBalance(Balance balance) throws Exception {
		this.getHibernateTemplate().update(balance);
	}

	@Override
	public Balance selectBalance(int userid) throws Exception {
		Query query = this.getSession().createQuery("from Balance where userid = ?");
		query.setParameter(0, userid);
		List<Balance> list = query.list();
			return list.isEmpty()?null:list.get(0);
	}

	@Override
	public List<Vipmember> selectVipmember(int userid) throws Exception {
		Query query = this.getSession().createQuery("from Vipmember where userid = ?");
		query.setParameter(0, userid);
		List<Vipmember> list = query.list();
			return list;
	}

	@Override
	public void selectTransaction(PagingUilt<Transaction> pu,int userid) throws Exception {
		int scount = scount(userid);
		pu.setLogCount(scount);
		
		Query query = this.getSession().createSQLQuery("select * from transaction where userid = ? order by id desc").addEntity(Transaction.class);
		query.setParameter(0, userid);
		int start=(pu.getPageNum()-1)*pu.getPageSize();
		int size = pu.getPageSize();
		query.setFirstResult(start);
		query.setMaxResults(size);
		List <Transaction>list = query.list();
		pu.setList(list);
	}
	private int scount(int userid) throws Exception {
		String sql = "select count(id) from transaction where userid = ?";
		Query query = this.getSession().createSQLQuery(sql);
		query.setParameter(0, userid);
		Integer size = Integer.parseInt(query.uniqueResult().toString());
		return size;
	}
	
	
	
	@Override
	public Keeptime selectKeeptime(int userid) throws Exception {
		Query query = this.getSession().createQuery("from Keeptime where userid = ?");
		query.setParameter(0, userid);
		List<Keeptime> list = query.list();
			return list.isEmpty()?null:list.get(0);
	}
	
	@Override
	public Keeptime selectKeeptime(int userid, int type,int device, int areaid)
			throws Exception {
		Query query = this.getSession().createQuery("from Keeptime where userid = ? and keep_type = ? and areaid = ? and keep_device = ?");
		query.setParameter(0, userid);
		query.setParameter(1, type);
		query.setParameter(2, areaid);
		query.setParameter(3, device);
		List<Keeptime> list = query.list();
			return list.isEmpty()?null:list.get(list.size()-1);
	}
	@Override
	public Keeptime selectKeeptime(int userid, int type)
			throws Exception {
		Query query = this.getSession().createQuery("from Keeptime where userid = ? and keep_type = ?");
		query.setParameter(0, userid);
		query.setParameter(1, type);
		List<Keeptime> list = query.list();
			return list.isEmpty()?null:list.get(0);
	}
	
	
	
	@Override
	public void insertKeeptime(Keeptime keeptime) throws Exception {
		this.getHibernateTemplate().save(keeptime);
		
	}
	@Override
	public void updateKeeptime(Keeptime keeptime) throws Exception {
		this.getHibernateTemplate().update(keeptime);
		
	}
	@Override
	public Balanceorder selectBalanceorder(int userid, String orderFrom)
			throws Exception {
		Query query = this.getSession().createQuery("from Balanceorder where userid = ? and orderfrom=?");
		query.setParameter(0, userid);
		query.setParameter(1, orderFrom);
		List<Balanceorder> list = query.list();
			return list.isEmpty()?null:list.get(0);
	}
	@Override
	public Serializable insertBalanceorder(Balanceorder balanceorder)
			throws Exception {
		return this.getHibernateTemplate().save(balanceorder);
	}
	@Override
	public Vipmember selVipmember(int userid, int vipnum) throws Exception {
		Query query = this.getSession().createQuery("from Vipmember where userid = ? and name_code = ?");
		query.setParameter(0, userid);
		query.setParameter(1, vipnum);
		List<Vipmember> list = query.list();
			return list.isEmpty()?null:list.get(0);
	}
	@Override
	public Viprecord selViprecord(int userid, int vipnum) throws Exception {
		Query query = this.getSession().createQuery("from Viprecord where userid = ? and name_code = ?");
		query.setParameter(0, userid);
		query.setParameter(1, vipnum);
		List<Viprecord> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}
	@Override
	public Serializable insetrViprecord(Viprecord viprecord) throws Exception {
		return this.getHibernateTemplate().save(viprecord);
	}
	@Override
	public Keeptime selectKeeptime(int userid, String netorder)
			throws Exception {
		Query query = this.getSession().createQuery("from Keeptime where userid = ? and net_order = ?");
		query.setParameter(0, userid);
		query.setParameter(1, netorder);
		List<Keeptime> list = query.list();
			return list.isEmpty()?null:list.get(0);
	}
	
}
